package cn.finder.datatunnel.server.test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Date;

public class ProcessorRunnable implements Runnable{

	
	private Socket socket;
	
	public ProcessorRunnable(Socket socket){
		this.socket=socket;
	}

	public void run() {


	   /*PrintWriter os=null;


       BufferedReader br=null;
		try {
			os=new PrintWriter(socket.getOutputStream());
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));


			String readline;

			readline=br.readLine();
			System.out.println("===服务器端读取到了数据:"+readline);
			
			os.print(readline+"==>"+new Date().toString());
			os.flush();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
         
		
		try {
			
			
			
			
			// 创建读取数据的Reader,里面指定了需要的编码类型。
		    /*  BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),
		          "UTF-8"));
		      
		      StringBuffer sb=new StringBuffer();
		      // 读取每一行的数据.注意大部分端口操作都需要交互数据。
		      String str;
		      while ((str = rd.readLine()) != null) {
		    	  sb.append(str);
		      }
			
			
			
			System.out.println("===服务器端读取到了数据:"+sb.toString());
			
			
			
			 DataOutputStream out = new DataOutputStream(
					 socket.getOutputStream());

			
			 out.write((sb.toString()+"==>"+new Date().toString()).getBytes("utf-8"));
			 out.flush();
			 out.close();*/
			
			
			byte[] readData=streamToBytes(socket.getInputStream(),"size");
			
			
			String readDataStr=new String(readData);
			System.out.println("===服务器端读取到了数据:"+readDataStr);
			
			 DataOutputStream out = new DataOutputStream(
					 socket.getOutputStream());

			 String retStr=(readDataStr+"==>"+new Date().toString()+"\n");
			 
			 if(readDataStr=="1")
				 retStr="1\n";
			 else{
				 retStr="0\n";
			 }
			 
			 out.write(retStr.getBytes("utf-8"));
			 out.flush();
			 out.close();
			 System.out.println("===end============");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		
	}
	/***
	 * 流转字节数组
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public  byte[] streamToBytes(InputStream is,String endmark) throws IOException{
		if("mark".equalsIgnoreCase(endmark)){
			byte dt_client_readdata_mark=Byte.valueOf("10").byteValue();
			
			
			byte[] b=new byte[1];
			
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			
			int pos=-1;
			while((pos=is.read(b, 0, b.length))!=-1){
				
				if(b[0]==dt_client_readdata_mark){
					break;
				}
				
				baos.write(b, 0, pos);
				
			}
			
			return baos.toByteArray();
		}
		
		
		else if("size".equalsIgnoreCase(endmark)){
			int dt_client_readdata_size=10;
			
			byte[] b=new byte[1];
			
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			
			int pos=-1;
			while((pos=is.read(b, 0, b.length))!=-1){
				baos.write(b, 0, pos);
				if(baos.size()==dt_client_readdata_size){
					break;
				}
				
			}
			
			return baos.toByteArray();
		}
		
		return null;
				
		
	}
}
